package cxp.utils;

import cxp.entity.TableColumns;
import cxp.entity.Tables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * Created by cxp on 2018/5/24.
 */
public class EntityUtils {
    /**
     * 获取该库所有表
     * @return
     */
    public static List<Tables> getAllTableName(){
        List<Tables> list = new ArrayList<>();
        Connection conn = JdbcUtils.getInstance().getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("show tables");
            rs = ps.executeQuery();
            while (rs.next()){
                String tbName = rs.getString(1);
                Tables tb = new Tables();
                tb.setTableName(tbName);
                list.add(tb);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    /**
     * 获取所有表字段
     * @return
     */
    public static List<Tables> getAllTablesColumns(){
        List<Tables> list = getAllTableName();
        List<Tables> result = new ArrayList<>();
        Connection conn = JdbcUtils.getInstance().getConn();
        try{
            Iterator<Tables> items = list.iterator();
            while (items.hasNext()) {
                Tables item = items.next();
                PreparedStatement ps = null;
                ResultSet rs = null;
                try {
                    ps = conn.prepareStatement("desc `" + item.getTableName() + "`");
                    rs = ps.executeQuery();
                    Set<TableColumns> set = new HashSet<>();
                    while (rs.next()) {
                        String tcName = rs.getString(1);
                        String type = rs.getString(2);
                        TableColumns tc = new TableColumns();
                        tc.setColumnName(tcName);
                        tc.setColumnType(type);
                        set.add(tc);
                    }
                    item.setColumns(set);
                    result.add(item);
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 生成实体类文件
     */
    public static void generatorPojo(){
        List<Tables> list = getAllTablesColumns();
        // TODO: 2018/5/25 生成文件
    }

}
